package com.wtb.qq.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tencent.weibo.beans.OAuth;
import com.tencent.weibo.utils.OAuthClient;
import com.wtb.domain.Config;
import com.wtb.pub.WtbException;
import com.wtb.pub.WtbBaseServlet;
/**
 * 
 * 1-获取未授权的RequestToken
 * 2-转发至授权页面
 * @author tacheno
 *
 */
@SuppressWarnings("serial")
public class QqConnServlet extends WtbBaseServlet {
	private static final Log log = LogFactory.getLog(QqConnServlet.class);
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		OAuth oauth = new OAuth(Config.QQ_APP_KEY,Config.QQ_APP_SECRET,Config.QQ_REDIRECT_URL+"/QqOauthServlet");
		OAuthClient client = new OAuthClient();
		try {
			oauth = client.requestToken(oauth);
			if (oauth.getStatus() == 1) {
				log.debug("获取未授权RequestToken失败！");
				response.sendRedirect("error?err_code=1002");
				return;
			}
			request.getSession().setAttribute("qqOauth", oauth);
			response.sendRedirect(Config.QQ_OAUTH_URL+"?oauth_token="+oauth.getOauth_token());
		} catch (Exception e) {
			log.error("获取未授权RequestToken失败：", e);
			response.sendRedirect("error?err_code=1002");
			throw new WtbException("获取未授权RequestToken失败"+e);
		}
	}

}
